473,441 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Capture Mouse Coordinates FireFox

Can Someone Please tell me why my script works in IE, but not in any other
browser>????
Thanks.
__________________________________________
var IE = document.all?true:false;

if (!IE) document.captureEvents(Event.onClick)
document.onClick = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}

somealert(tempX);
return true;
}

function somealert(tempX) {
window.alert(tempX);
}
_________________________________________
Dec 7 '05 #1
2 16926
John wrote:
Can Someone Please tell me why my script works in IE, but not in any other
browser>????
Thanks.
__________________________________________
var IE = document.all?true:false;
You are assuming that any browser that has detectable support for
document.all also implements the IE event model - that doesn't seem like
a reasonable idea.

Why not test for the feature you are using?

if (!IE) document.captureEvents(Event.onClick)
if (document.captureEvents) document.captureEvents(Event.onClick);
Though I think that is completely unnecessary.

document.onClick = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
Ditch that line, use:

var e = e || window.event;

tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}


Again, feature detection is the go:

if ('number' == typeof e.pageX){
tempX = e.pageX;
tempY = e.pageY;
} else if ('number' == typeof e.pageX){
tempX = e.clientX + document.body.scrollLeft;
tempY = e.clientY + document.body.scrollTop;
}

Assumes support for document.body.scrollLeft if e.pageX is supported.
You may wish to further test that.
[...]
--
Rob
Dec 7 '05 #2
"John" <vi****@REMOVEiprimus.com.au> writes:
Can Someone Please tell me why my script works in IE, but not in any other
browser>????
Because it is specifically written to work in IE 4 and Netscape 4.
Accidentally, it also works in later versions of IE.

The script is most likely 5+ years old, and makes assumptions that
were wrong even then (that IE and NS 4 are the only existing browsers).
Today, it's dangerously wrong.

var IE = document.all?true:false;
This suggests that the presence of document.all *guarantees* that the
browser is IE. That is incorrect.

Detecting the browser was never a good idea, since, as a strategy, it
is almost certain to fail when new browsers appear.

The recommended strategy is feature detection: check for the exact
feature you need, and don't try to infer it from a flawed browser
detection.

if (!IE) document.captureEvents(Event.onClick)
This line assumes that any non-IE browser is Netscape 4. Since that
is not the case, this line might give an error in browsers that
test as non-IE.

A safer way to support Netscape 4 (the only browser requireing
this line, and not one I would support at all) would be:

if (document.captureEvents && this.Event && Event.onClick) {
document.captureEvents(Event.onClick);
}
document.onClick = getMouseXY;
I am surprised that this works at all, since the "onclick" event
handler is written with without capital letters.
var tempX = 0;
var tempY = 0;
Should these really be global variables?
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;


again the assumption that anything that tests as IE (has a
document.all) will also implement IE's event handling, including the
global event object.

Instead do something like:

e = e || window.event; // support IE's global event object

if (typeof e.pageX == "number") {
tempX = e.pageX;
tempY = e.pageY;
} else if (typeof e.clientX == "number" &&
typeof document.body.scollLeft == "number") {
tempX = e.clientX + document.body.scrollLeft;
tempY = e.clientY + document.body.scrollTop;
} else {
// panic;
}
Generally, the script suffers from the flawed assumption that there
are exactly two browsers, and that document.all distinguishes them.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcia Gulesian | last post by:
How can I capture the event when I click (focus) with the cursor anywhere in the page (that is, on a component or elsewhere). This event would occur in an I.E 5.5 or later browser.
4
by: Jay | last post by:
Hi, How can I capture mouse position on Image? I found number of script capturing mouse position of the page. But I could not find anything based on image. What I want to find out is X Y...
4
by: Jonne | last post by:
Hi, I haven't found anything like this anywhere with Google, so I'm posting it here, hoping one of you people knows how to do something like this. I'm trying to get the mouse coordinates in a div,...
2
by: quickcur | last post by:
Hi, I have html like this: <div id="myCanvas" style="border:10px, black;position:relative;height:250px;width:100%;"> <img id="p" src="p.jpg"> </div> When user click the mosue, I would like ...
2
by: cefrancke | last post by:
Is there a way to capture all relevant info about the mouse, without using Mouse Up/Down etc. procedures? I'm trying to make a custom function and send it the mouse info on the click event. ...
2
Mobius Evalon
by: Mobius Evalon | last post by:
I'm trying to make this thing work in Firefox, because I know the client my game is on is pondering switching to Gecko (they're currently using something very similar to IE4 for browser windows...
2
by: romain.larmet | last post by:
Hi all, I need to get the mouse cursor's position using JavaScript. Until now, everything was working well in IE, FF and Opera using the event.clientX/Y and e.pageX/Y members, depending on which...
4
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have a winform containing a scrollable panel and a groupbox inside the panel. There is a button inside the groupbox. When that button is clicked; how do I capture and display the X and Y...
4
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.